import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileOutputStream;
import java.io.IOException;

public class ColorListGenerator {

    public static void main(String[] args) {
        // 创建一个新的工作簿
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("Color List");

        // 获取所有 IndexedColors 枚举值
        IndexedColors[] indexedColors = IndexedColors.values();

        // 创建表头
        Row headerRow = sheet.createRow(0);
        headerRow.createCell(0).setCellValue("Font Color (Code)");
        headerRow.createCell(1).setCellValue("Fill Color (Code)");

        // 设置列宽
        sheet.setColumnWidth(0, 5000); // 第一列宽度
        sheet.setColumnWidth(1, 5000); // 第二列宽度

        // 遍历所有颜色并填充表格
        for (int i = 0; i < indexedColors.length; i++) {
            IndexedColors color = indexedColors[i];
            short colorIndex = color.getIndex();

            // 创建一行
            Row row = sheet.createRow(i + 1);

            // 第一列：字体颜色
            Cell fontColorCell = row.createCell(0);
            fontColorCell.setCellValue(color.name() + " (" + colorIndex + ")");
            CellStyle fontColorStyle = createFontCellStyle(workbook, colorIndex);
            fontColorCell.setCellStyle(fontColorStyle);

            // 第二列：背景颜色
            Cell fillColorCell = row.createCell(1);
            fillColorCell.setCellValue(color.name() + " (" + colorIndex + ")");
            CellStyle fillColorStyle = createFillCellStyle(workbook, colorIndex);
            fillColorCell.setCellStyle(fillColorStyle);
        }

        // 保存文件到桌面
        String desktopPath = System.getProperty("user.home") + "/Desktop/ColorList.xlsx";
        try (FileOutputStream fileOut = new FileOutputStream(desktopPath)) {
            workbook.write(fileOut);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                workbook.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        System.out.println("颜色列表已生成，文件路径：" + desktopPath);
    }

    /**
     * 创建字体颜色样式
     */
    private static CellStyle createFontCellStyle(Workbook workbook, short colorIndex) {
        CellStyle style = workbook.createCellStyle();
        Font font = workbook.createFont();
        font.setColor(colorIndex);
        style.setFont(font);
        return style;
    }

    /**
     * 创建背景颜色样式
     */
    private static CellStyle createFillCellStyle(Workbook workbook, short colorIndex) {
        CellStyle style = workbook.createCellStyle();
        style.setFillForegroundColor(colorIndex);
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        return style;
    }
}